package com.funny.stock.trade.controller;

import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.base.vo.TradeType;
import com.funny.stock.trade.service.ComposeStockActionService;
import com.funny.stock.trade.service.EmulateComposeService;
import com.funny.stock.trade.vo.ComposeStockActionQueryVO;
import com.funny.stock.trade.vo.ComposeStockActionVO;
import com.funny.stock.trade.vo.EmulateComposeVO;
import com.funny.stock.user.util.UserSecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengl
 * @date 2020/05/17
 **/
@RestController
@RequestMapping("/compose/stock/action")
public class ComposeStockActionController {
    public static final Logger logger = LoggerFactory.getLogger(ComposeStockActionController.class);

    @Autowired
    ComposeStockActionService stockActionService;

    @Autowired
    EmulateComposeService emulateComposeService;

//   @Autowired
//    ComposeAssetsService composeAssetsService;

    @PostMapping("/trade")
    public Result<ComposeStockActionVO> buy(@RequestBody ComposeStockActionVO dataObj){
        Long userId = UserSecurityUtil.getUserId();
        if(dataObj.getComposeId() == null)
            return ResultUtil.error("执行交易失败,模拟组合ID为空");

        if(dataObj.getStockCode() == null)
            return ResultUtil.error("执行交易失败,成交股票代码为空");

        if(dataObj.getStockName() == null)
            return ResultUtil.error("执行交易失败,成交股票名称为空");

        TradeType tradeType = TradeType.of(dataObj.getAction());
        if(tradeType == null)
            return ResultUtil.error("执行交易失败,不支持的交易方式");

        if(dataObj.getActionPrice() == null)
            return ResultUtil.error("执行交易失败,成交价格为空");

        if(dataObj.getActionNumber() == null)
            return ResultUtil.error("执行交易失败,成交数量为空");

        EmulateComposeVO composeVO = emulateComposeService.findByCreateIdAndId(userId, dataObj.getComposeId());
        if(composeVO == null || (composeVO.getIsDelete() != null && composeVO.getIsDelete() == 1)){
            return ResultUtil.error("执行交易失败,模拟组合不存在");
        }

//        ComposeAssetsVO assetsVO = composeAssetsService.findByComposeId(composeVO.getId());
//        if(assetsVO == null)
//            return ResultUtil.error("执行交易失败,资产不存在");



        return  stockActionService.doTradeAction(userId, dataObj, composeVO);
    }


    @PostMapping("/queryByPage")
    public Result query(@RequestBody ComposeStockActionQueryVO dataObj){
        dataObj.setCreateId(UserSecurityUtil.getUserId().intValue());
        Page<ComposeStockActionVO> page = stockActionService.queryByCondition(dataObj);
        return ResultUtil.success(page);
    }

}
